---@class UIBP_MainUI_C:UUserWidget
---@field Player01 UUIBP_PlayerListItem_C
---@field Player02 UUIBP_PlayerListItem_C
---@field Player03 UUIBP_PlayerListItem_C
---@field Player04 UUIBP_PlayerListItem_C
---@field UIBP_AdjustCard UUIBP_AdjustCard_C
---@field UIBP_AdjustTime UUIBP_AdjustTime_C
---@field UIBP_ChooseAdjustCard UUIBP_ChooseAdjustCard_C
---@field UIBP_ChooseTile UUIBP_ChooseTile_C
---@field UIBP_ChooseTile_0 UUIBP_ChooseTile_C
---@field UIBP_GameStartTips UUIBP_GameStartTips_C
---@field UIBP_Lose UUIBP_Lose_C
---@field UIBP_Prepare UUIBP_Prepare_C
---@field UIBP_RoundCD UUIBP_RoundCD_C
---@field UIBP_RoundOperation UUIBP_RoundOperation_C
---@field UIBP_RoundTips UUIBP_RoundTips_C
---@field UIBP_ShowCard UUIBP_ShowCard_C
---@field UIBP_Win UUIBP_Win_C
--Edit Below--
require("Script.GameConfigs.GlobalConfigs");
require("Script.Common.Debug")
local UIBP_MainUI = { 
	-- mark game start
    BGameStart = true;
}; 

--Construct
function UIBP_MainUI:Construct()
	ugcprint("UIBP_MainUI:Construct");
    self.bCanEverTick = true 

    UICommonFunctionLibrary.SetAdaptation(self.CanvasPanel_0, self.CanvasPanel_0)
    
    self:InitUI();
    self:InitBindEvent();

  
end
-- Construct 

function UIBP_MainUI:Tick(MyGeometry, InDeltaTime)
    -- if miss prepare stage and in a rouond now, need to update UI by yourself
    if self.CurTeamIndex == nil and UGCGameSystem.GameState.CurRoundTeamIndex ~= 0 then
        local PlayerInfo = UGCGameSystem.GameState:GetCurRoundPlayerInfo()
        if PlayerInfo ~= nil then
            -- call OnPlayerStartNewRound by yourself , update UI
            print("UIBP_MainUI:ReceiveTick Manually Call OnPlayerStartNewRound");
            self:OnPlayerStartNewRound(UGCGameSystem.GameState.CurRoundTeamIndex, PlayerInfo.PlayerKey, false)
        end
    end
    
end

function UIBP_MainUI:InitUI()
    -- register 
    -- 4 player info panel
    self.PlayerID = {
        self.Player01;
        self.Player02;
        self.Player03;
        self.Player04;
    }
   
    -- 6 card
    self.Cards = {
        self.UIBP_ShowCard.Card1;
        self.UIBP_ShowCard.Card2;
        self.UIBP_ShowCard.Card3;
        self.UIBP_ShowCard.Card4;
        self.UIBP_ShowCard.Card5;
        self.UIBP_ShowCard.Card6;
    }

    -- set some UI unvisible
    self.UIBP_AdjustCard:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_Lose:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_RoundTips:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_Win:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_GameStartTips:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_RoundCD:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_AdjustTime:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_ChooseAdjustCard:SetVisibility(ESlateVisibility.Collapsed)
    self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.Collapsed)
    
end


-- 
function UIBP_MainUI:InitBindEvent( ... )
    print("UIBP_MainUI:InitBindEvent");
	-- UGCEventSystem:AddListener(AeroplaneChessEventType.TeamIndexAssigned, self.OnTeamIndexAssigned, self);
    UGCEventSystem:AddListener(EventType.PlayerInfosChanged, self.OnPlayerInfosChanged, self);
    UGCEventSystem:AddListener(EventType.PlayerPanelChange, self.OnShowPlayerPanel, self);
    UGCEventSystem:AddListener(EventType.GameStartChanged, self.OnGameStartChanged, self);
    UGCEventSystem:AddListener(EventType.CurTeamIndexChanged, self.ForceUpdateUI, self);
    UGCEventSystem:AddListener(EventType.PrepareStageRemainTimeChanged, self.OnPrepareStageRemainTimeChanged, self);
    UGCEventSystem:AddListener(EventType.CurRoundRemainTimeChanged, self.OnCurRoundRemainTimeChanged, self);
    UGCEventSystem:AddListener(EventType.CurRoundStatusChanged, self.ForceUpdateUI, self);
    UGCEventSystem:AddListener(EventType.PlayerStartNewRound, self.OnPlayerStartNewRound, self);
    UGCEventSystem:AddListener(EventType.PlayCard, self.OnPlayCard, self);
    UGCEventSystem:AddListener(EventType.UpdateCardUI, self.OnUpdateCardUI, self);
    --UGCEventSystem:AddListener(EventType.PlayerFlyPlane, self.OnPlayerFlyPlane, self);
    UGCEventSystem:AddListener(EventType.AdjustCardUI, self.OnAdjustCardUI, self);
    UGCEventSystem:AddListener(EventType.SelectCardUI, self.ShowCardSelectionPanel, self);
    UGCEventSystem:AddListener(EventType.AdjustRemainTimeChanged, self.OnAdjustRemainTimeChanged, self);
    UGCEventSystem:AddListener(EventType.CanAchieveTiles,self.OnCanAchieveTilesChanged,self);

    -- select confirm button
    self.UIBP_RoundOperation.Button_Confirm.OnClicked:Add(self.SelectCard_Button_OnClicked, self)
    self.UIBP_ChooseTile_0.Button_Confirm.OnClicked:Add(self.ChooseTile_Button_OnClicked,self)

    -- 六个卡牌按钮
    print("UIBP_MainUI:InitBindEvent bind card button start")
    for i,card in pairs(self.Cards) do
        card.Button_0.OnClicked:Add(function()self:Card_OnClicked(i)end)
    end
    
    --self.UIBP_ShowCard.Card1.Button_0.OnClicked:Add(self.Card_OnClicked,self,1);
    print("UIBP_MainUI:InitBindEvent bind card button end")

end


function UIBP_MainUI:ReceivePreDestroy()
    ugcprint("UIBP_MainUI:ReceivePreDestroy")
    self:UnBindEvents();
end

function UIBP_MainUI:UnBindEvents()
    -- UGCEventSystem:RemoveListener(AeroplaneChessEventType.TeamIndexAssigned, self.OnTeamIndexAssigned, self);
    UGCEventSystem:RemoveListener(EventType.PlayerInfosChanged, self.OnPlayerInfosChanged, self);
    UGCEventSystem:RemoveListener(EventType.PlayerPanelChange, self.OnShowPlayerPanel, self);
    UGCEventSystem:RemoveListener(EventType.GameStartChanged, self.OnGameStartChanged, self);
    UGCEventSystem:RemoveListener(EventType.CurTeamIndexChanged, self.ForceUpdateUI, self);
    UGCEventSystem:RemoveListener(EventType.PrepareStageRemainTimeChanged, self.OnPrepareStageRemainTimeChanged, self);
    UGCEventSystem:RemoveListener(EventType.CurRoundRemainTimeChanged, self.OnCurRoundRemainTimeChanged, self);
    UGCEventSystem:RemoveListener(EventType.CurRoundStatusChanged, self.ForceUpdateUI, self);
    UGCEventSystem:RemoveListener(EventType.PlayerStartNewRound, self.OnPlayerStartNewRound, self);
    UGCEventSystem:RemoveListener(EventType.PlayCard, self.OnPlayCard, self);
    UGCEventSystem:RemoveListener(EventType.UpdateCardUI, self.OnUpdateCardUI, self);
    --UGCEventSystem:RemoveListener(EventType.PlayerFlyPlane, self.OnPlayerFlyPlane, self);
    UGCEventSystem:RemoveListener(EventType.AdjustCardUI, self.OnAdjustCardUI, self);
    UGCEventSystem:RemoveListener(EventType.SelectCardUI, self.ShowCardSelectionPanel, self);
    UGCEventSystem:RemoveListener(EventType.AdjustRemainTimeChanged, self.OnAdjustRemainTimeChanged, self);
    UGCEventSystem:RemoveListener(EventType.CanAchieveTiles,self.OnCanAchieveTilesChanged,self);

    self.UIBP_RoundOperation.Button_Confirm.OnClicked:RemoveAll(self.SelectCard_Button_OnClicked, self);
    self.UIBP_ChooseTile_0.Button_Confirm.OnClicked:RemoveAll(self.ChooseTile_Button_OnClicked, self);
end



-- function UIBP_MainUI:Destruct()

-- end

-- 
function UIBP_MainUI:OnPlayerInfosChanged(playerInfo)
    log_tree_dev(playerInfo)
    --self:UpdateAutoPlayUI()
    --self:UpdateWatchingUI(playerInfo);
    self:OnShowPlayerPanel(playerInfo);
end

-- 点击卡牌以后的操作，本来写在CardItem里的，但是好像没反应
-- 
function UIBP_MainUI:Card_OnClicked(cardIndex)
    print("UIBP_MainUI:Card_OnClicked  ".. cardIndex)
    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    if PlayerController ~= nil then
        local teamIndex = PlayerController.PlayerState.TeamIndex;
        if teamIndex ~= nil then
            -- 设置该用户选中的卡牌
            
            UGCGameSystem.GameState.PlayerInfos[teamIndex].CardChoosed = self.Cards[cardIndex].CardType;
            print("UIBP_MainUI:Card_OnClicked. teamIndex  "..teamIndex.."  "..self.Cards[cardIndex].CardType)
        else
            print("UIBP_CardItem:Card_OnClicked get TeamIndex failed");
        end
    else
        print("UIBP_CardItem:Card_OnClicked get playerController failed");
    end
end


-- 
function UIBP_MainUI:OnShowPlayerPanel(PlayerInfos)
    print("UIBP_MainUI:OnShowPlayerPanel")
    for i, PlayerState in ipairs(PlayerInfos) do

        if PlayerInfos[i].PlayerName ~= nil then
            self.PlayerID[i].TB_Name:SetText(PlayerInfos[i].PlayerName)
            --self.PlayerID[i].TextBlock_Rank:SetText(i)
        end
        self.PlayerID[i].WidgetSwitcher_Tips:SetActiveWidgetIndex(i - 1)
        -- player in which level 
        self.PlayerID[i].TB_Level:SetText(0)
        -- player has how many cards
        self.PlayerID[i].TB_CardNum:SetText(PlayerInfos[i].CardNum)
    end
    -- 更新自己的卡牌
    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    print(string.format("playerpadkg[%d]",PlayerController.PlayerState.TeamIndex))--4
    if PlayerController ~= nil then
        local teamIndex = PlayerController.PlayerState.TeamIndex;--一次为4
        if teamIndex ~= nil then
            --获取自己的卡牌
            local cardInfos = PlayerInfos[teamIndex].CardInfos
            for i,type in ipairs(cardInfos) do
                --self.Cards[i].Image_0.SetBrushFromAsset(CardSmallImg[cardInfos[i]]);
                self.Cards[i].CardType = cardInfos[i];
                print("UIBP_MainUI:OnShowPlayerPanel set CardType "..self.Cards[i].CardType)

                local path =UGCMapInfoLib.GetRootLongPackagePath() ..  CardSmallImg[cardInfos[i]]
                print(path)
                local texture = UE.LoadObject(path)
                self.Cards[i].Image_0:SetBrushFromTexture(texture);

            end
        else
            print("UIBP_MainUI:OnShowPlayerPanel get TeamIndex failed");
        end
    else
        print("UIBP_MainUI:OnShowPlayerPanel get playerController failed");
    end
end

-- Game start tips
function UIBP_MainUI:OnGameStartChanged()
    self.UIBP_GameStartTips:Show(true);
end

-- update UI.没调用？？？？？？Update卡牌UI？？？？？？？？？
function UIBP_MainUI:ForceUpdateUI()
    
    if UGCGameSystem.GameState == nil or Mode == nil or Mode.OwnerPlayerState == nil then
        return;
    end
    print("sjdiwin")
    if UGCGameSystem.GameState.CurrentGamestate == EGameStatus.Gaming then
        print("sdjiogreg")
        if Mode.IsTeamIndexValid(UGCGameSystem.GameState.CurRoundTeamIndex) then
            print("dgjdgi")
            print(string.format("dfgrghtrgyrtr[%d]",UGCGameSystem.GameState.CurRoundTeamIndex))--1,第一个玩家
            self.CurTeamIndex = UGCGameSystem.GameState.CurRoundTeamIndex
            self.IsMyRound = self.CurTeamIndex == Mode.OwnerPlayerState.TeamIndex
            print(string.format("AC_Main_UI:ForceUpdateUI: P[%d] IsMyRound:%s", self.CurTeamIndex, tostring(self.IsMyRound)));
            -- choose card button
            local shouldShowSelectCard = self.IsMyRound and UGCGameSystem.GameState.CurRoundStatus == ERoundStatus.WaitForSelectCard
            self.UIBP_RoundOperation:SetVisibility(shouldShowSelectCard and ESlateVisibility.SelfHitTestInvisible or ESlateVisibility.Collapsed)
            -- ?ɻ?ѡ??????
            --local shouldShowPlaneSelection = self.IsMyRound and UGCGameSystem.GameState.CurRoundStatus == ERoundStatus.WaitForPlaneSelection
            --self:ShowPlaneSelectionPanel(shouldShowPlaneSelection)
            -- ֻ??????Ϸ?н׶β???Ҫ?????й?UI
            --self:UpdateAutoPlayUI()
        end
    else
        self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.Collapsed)
        --self:ShowPlaneSelectionPanel(false)
    end
end

-- ׼update prepare stage remain time
function UIBP_MainUI:OnPrepareStageRemainTimeChanged(RemainTime)
    ugcprint(string.format("UIBP_MainUI:OnPrepareStageRemainTimeChanged RemainTime[%f]", RemainTime))
   --when RemainTime<0, set cd UI unvisible
    if RemainTime > 0 then
        self.UIBP_Prepare:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
        self.UIBP_Prepare.TB_time:SetText(tostring(math.ceil(RemainTime)))
    else
        self.UIBP_Prepare:SetVisibility(ESlateVisibility.Collapsed);
        -- unset highlight
        -- local myTeamIndex = self:GetMyTeamIndex()
        -- if myTeamIndex then
        --     for index = 1, 4 do
        --         local player = UGCGameSystem.GameState.PlayerInfos[myTeamIndex].PlaneInfos[index]
        --         player:GetCurTile():SetTileHighlight(false)
        --     end
        -- end
	end
end

-- update current round cd 
function UIBP_MainUI:OnCurRoundRemainTimeChanged(RemainTime)
    if UGCGameSystem.GameState.ShowRemainTime then
        ugcprint(string.format("UIBP_MainUI:OnCurRoundRemainTimeChanged RemainTime[%f]", RemainTime))
        self.UIBP_RoundCD:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
        self.UIBP_RoundCD.TB_time:SetText(""..math.ceil(RemainTime))

        -- 仅在当前玩家页面显示,选择卡牌页面
        local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
        if PlayerController ~= nil then
            local teamIndex = PlayerController.PlayerState.TeamIndex;
            if teamIndex ~= nil and teamIndex == UGCGameSystem.GameState.CurRoundTeamIndex then
                self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
            else
                print("UIBP_MainUI:OnCurRoundRemainTimeChanged get TeamIndex failed");
            end
        else
            print("UIBP_MainUI:OnCurRoundRemainTimeChanged get playerController failed");
        end

        if RemainTime <= 0 then
            self.UIBP_RoundCD:SetVisibility(ESlateVisibility.Collapsed);
            self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.Collapsed);
        end
    else
        if UE.IsValid(self) then
            self.UIBP_RoundCD:SetVisibility(ESlateVisibility.Collapsed);
        end
    end
end

-- player start a round
function UIBP_MainUI:OnPlayerStartNewRound(TeamIndex, PlayerKey, IsPlayAnotherRound)
    if not UE.IsValid(self) then return end
    if self.CurTeamIndex == TeamIndex and not IsPlayAnotherRound then return end
    ugcprint(string.format("UIBP_MainUI:OnPlayerStartNewRound: P[%d]", TeamIndex));
    -- do not update UI here
    -- self:ForceUpdateUI()
    self.IsMyRound = PlayerKey == Mode.OwnerPlayerKey
    self.CurTeamIndex = TeamIndex

    --TODO RoundNum and AliveNum have not defined.
    --测试时先用1和4替代
    --self.UIBP_RoundTips:Show(true, TeamIndex, PlayerKey,RoundNum,AliveNum);
    self.UIBP_RoundTips:Show(true, TeamIndex, PlayerKey,1,4);


    -- ͬsame player, choose another card 
    if IsPlayAnotherRound then
        self:OnPlayerStartAnotherRound(true, TeamIndex, PlayerKey)
    end
end

-- play card
-- 目前只是关闭选择卡牌通知，后续加上动画
function UIBP_MainUI:OnPlayCard(TeamIndex, CardInfo)
    print(string.format("UIBP_MainUI:OnReceivedDiceResult: P[%d] Result[%d]", TeamIndex,CardInfo));
    --ugcprint(UGCGameSystem.GameState.DiceActor:GetAnimPathWithResult(0));
    -- show  dice process and result 
   --[[ local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    UGCAsyncLoadTools:LoadObject(UGCGameSystem.GameState.DiceActor:GetAnimPathWithResult(0), 
        function (ProcessBrush)
            if UE.IsValid(self) then
                self.AC_DiceShow_UIBP.DiceShow_Image:SetBrushFromAsset(ProcessBrush);
            end
        end
    )--]]--
    --[[UGCAsyncLoadTools:LoadObject(UGCGameSystem.GameState.DiceActor:GetAnimPathWithResult(DiceResult), 
        function (ResultBrush)
            if UE.IsValid(self) then
                self.AC_DiceShow_UIBP.ResultBrush = ResultBrush;
            end
        end
    )]]--
    --local ProcessBrush = UE.LoadObject(UGCGameSystem.GameState.DiceActor:GetAnimPathWithResult(0));

    --local ResultBrush = UE.LoadObject(UGCGameSystem.GameState.DiceActor:GetAnimPathWithResult(DiceResult));

    -- play music
    --[[UGCGameSystem.GameState.DiceActor:PlayRollDiceVoice();
    if not UE.IsValid(self) then return end
    -- self.AC_DiceShow_UIBP.DiceShow_Image:SetBrushFromAsset(ProcessBrush);
    -- self.AC_DiceShow_UIBP.ResultBrush = ResultBrush
    self.AC_DiceShow_UIBP.Result = DiceResult
    self.AC_DiceShow_UIBP:Show(true);
    --]]--

    if self.IsMyRound then
        self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.Collapsed)
        self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
    end
end

-- set choose card UI visible
function UIBP_MainUI:ShowCardSelectionPanel(TeamIndex, PlayerKey)
    print("UIBP_MainUI:ShowCardSelectionPanel");
    --self.UIBP_RoundOperation:SetVisibility(Show and ESlateVisibility.SelfHitTestInvisible or ESlateVisibility.Collapsed)
    -- 仅在当前玩家页面显示
    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    if PlayerController ~= nil then
        local teamIndex = PlayerController.PlayerState.TeamIndex;
        if teamIndex ~= nil and teamIndex == TeamIndex then
            self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
            print("UIBP_MainUI:ShowCardSelectionPanel success  "..teamIndex)
        else
            print("UIBP_MainUI:ShowCardSelectionPanel get TeamIndex failed");
        end
    else
        print("UIBP_MainUI:ShowCardSelectionPanel get playerController failed");
    end
end

-- Client show owning cards UI,according to player's cardnum
function UIBP_MainUI:OnUpdateCardUI(PlayerInfos)
    
end

--player choose card again
function UIBP_MainUI:OnPlayerStartAnotherRound(bFind, TeamIndex, PlayerKey)
    print(string.format("UIBP_MainUI:OnPlayerStartAnotherRound : %s", ETeameColor[TeamIndex]))
    self.UIBP_RoundTips:ShowAgain(bFind, TeamIndex, PlayerKey,RoundNum,AliveNum);
end

--Ask player whether to adjust card
function UIBP_MainUI:OnAdjustCardUI()
    print("UIBP_MainUI:OnAdjustCardUI")
    self.UIBP_AdjustCard:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
    -- self.UIBP_AdjustCard.CanvasPanel_Choose:SetVisibility(ESlateVisibility.Collapsed)
    -- self.UIBP_AdjustCard.TB_time:SetText(tostring(math.ceil(RemainTime)))

end

function UIBP_MainUI:OnAdjustRemainTimeChanged(RemainTime)
    ugcprint(string.format("UIBP_MainUI:OnAdjustRemainTimeChanged[%f]", RemainTime))
    if RemainTime > 0 and RemainTime<=30 then
        self.UIBP_AdjustTime:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
        self.UIBP_AdjustTime.TB_time:SetText(tostring(math.ceil(RemainTime)))
        -- if not return nil,else return the card that player want to remove
        if self.UIBP_AdjustCard.WantToAdjust == 0 then 
            self.UIBP_AdjustCard:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
        else 
            if self.UIBP_AdjustCard.WantToAdjust == 1 then
                --TODO
                self.UIBP_AdjustCard:SetVisibility(ESlateVisibility.Collapsed)
                self.UIBP_ChooseAdjustCard:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
            -- elseif self.UIBP_AdjustCard.WantToAdjust == -1 then 
            --     self.UIBP_Prepare:SetVisibility(ESlateVisibility.Collapsed);
            end
        end
        
    else
        self.UIBP_AdjustTime:SetVisibility(ESlateVisibility.Collapsed);
        self.UIBP_AdjustCard:SetVisibility(ESlateVisibility.Collapsed);
        self.UIBP_ChooseAdjustCard:SetVisibility(ESlateVisibility.Collapsed);
    end
end

function UIBP_MainUI:OnCanAchieveTilesChanged(CanAchieveTiles,show)
    Debug:Log("UIBP_MainUI:OnCanAchieveTilesChanged start",show) --横车，这个往后不执行了

    for index, value in ipairs(CanAchieveTiles) do
        Debug:Log(index)
        if value~=0 then
            Debug:Log(KismetSystemLibrary.GetDisplayName(CanAchieveTiles[index]))
            break
        else
            Debug:Log("nil")
        end
        --[[
        local playerpawn = UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].PlayerPawn
        table.insert(UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete,playerpawn:GetCurTile().ID)
        playerpawn:GetController():FinishMove()
        ]]--
    end
    


    -- 仅在当前玩家页面显示
    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    if PlayerController ~= nil then
        local teamIndex = PlayerController.PlayerState.TeamIndex; -- 空？
        if teamIndex ~= nil and teamIndex == UGCGameSystem.GameState.CurRoundTeamIndex then
            Debug:Log("UIBP_MainUI:OnCanAchieveTilesChanged:it is my turn and show my Tiles")
            Debug:Log(CanAchieveTiles)
            for index, value in ipairs(CanAchieveTiles) do
                print(index)
                --Debug:Log("SetTileHighlight",KismetSystemLibrary.GetDisplayName(self.TileList[CanAchieveTiles[index]]))
                --self.TileList[CanAchieveTiles[index]]:SetTileHighlight(true);-- nil
                if value~=0 then
                    CanAchieveTiles[index]:SetTileHighlight(show);
                    Debug:Log("UIBP_MainUI:OnCanAchieveTilesChanged 0 ",KismetSystemLibrary.GetDisplayName(CanAchieveTiles[index])) --
                else 
                    Debug:Log("UIBP_MainUI:OnCanAchieveTilesChanged 1 nil  ",index)
                end
                
            end
            --[[
            for index, value in ipairs(CanAchieveTiles) do
                Debug:Log(index)
                if value~=0 then
                    Debug:Log(KismetSystemLibrary.GetDisplayName(CanAchieveTiles[index]))
                    break
                else
                    Debug:Log("nil")
                end
                local playerpawn = UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].PlayerPawn
                print("playerpawn get there")
                table.insert(UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete,playerpawn:GetCurTile().ID)
                print("insert get there")
                playerpawn:GetController():FinishMove()
            end
            ]]--
        else
            Debug:Log("UIBP_MainUI:OnCanAchieveTilesChanged:it is not my turn")
        end
    else
        print("UIBP_MainUI:OnCanAchieveTilesChanged get playerController failed");
    end
end

--[[------------------------------------------UI click event------------------------------------------------------]]--

-- choose card button 
--TODO
function UIBP_MainUI:SelectCard_Button_OnClicked()
        -- 用户点击的卡牌就存在UGCGameSystem.GameState.PlayerInfos[teamIndex].CardChoosed里，所以点击确认只是开始执行下一步
        print("UIBP_MainUI:SelectCard_Button_OnClicked")

        local CardInfo
    
     
        local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
        if PlayerController ~= nil then
            local teamIndex = PlayerController.PlayerState.TeamIndex;
            if teamIndex ~= nil then
               
                CardInfo = UGCGameSystem.GameState.PlayerInfos[teamIndex].CardChoosed;
                -- TODO：如果用户点击确认之前没有选择卡牌
                print("UIBP_MainUI:SelectCard_Button_OnClicked. teamIndex  "..teamIndex.."  "..CardInfo)
            else
                print("UIBP_MainUI:SelectCard_Button_OnClicked get TeamIndex failed");
            end
        else
            print("UIBP_MainUI:SelectCard_Button_OnClicked get playerController failed");
        end
    
        -- client for client self, CardInfo
        UnrealNetwork.CallUnrealRPC(Mode.OwnerController, Mode.OwnerController, "ServerRPC_SelectCard",CardInfo);
        --UnrealNetwork.CallUnrealRPC(Mode.OwnerController, Mode.OwnerController, "ServerRPC_ShowSelectCardTips", false);
        -- close remaintimeUI
        
        UnrealNetwork.CallUnrealRPC(Mode.OwnerController, Mode.OwnerController, "ServerRPC_ShowRemainTimeTips", false);
    
        print("UIBP_MainUI:SelectCard_Button_OnClicked RPC finished")
        -- 选择完以后把选择卡牌的页面关闭了
        --self.UIBP_RoundOperation:SetVisibility(ESlateVisibility.Collapsed);
        -- 打开选择地砖页面
        --self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
  
       
end

function UIBP_MainUI:ChooseTile_Button_OnClicked()
    Debug:Log("UIBP_MainUI:ChooseTile_Button_OnClicked")
    print("UIBP_MainUI:ChooseTile_Button_OnClicked")
    -- 以下两行仅用于测试，正式运行删掉，
    -- UGCGameSystem.GameState:PlayerFinishedMove();
    -- self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.Collapsed);


    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    if PlayerController ~= nil then
        print("ChooseTile_Button_OnClicked11")
        local teamIndex = PlayerController.PlayerState.TeamIndex;-- 为空？？？？
        print("ChooseTile_Button_OnClicked22")
        local direction = UGCGameSystem.GameState:CheckIsTileAchieved(UGCGameSystem.GameState.PlayerInfos[teamIndex].TileChoosed) -- 绊在这儿
        print("ChooseTile_Button_OnClicked33"..UGCGameSystem.GameState.PlayerInfos[teamIndex].TileChoosed.ID)
        if teamIndex ~= nil then
            -- 选择好砖块以后，RPC调用通知服务器1.判断砖块是不是可以到达；2.如果可以开始移动
            Debug:Log("Try to chosse card")
            Debug:Log(direction)--direction总是为-1，更改成功，一次为2
            if direction ~= -1 then
                Debug:Log("Move To", UGCGameSystem.GameState.PlayerInfos[teamIndex].TileChoosed)
                self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.Collapsed);
                -- 执行移动
                -- 请求移动棋子,在这里给方向和砖块赋值
                UnrealNetwork.CallUnrealRPC(Mode.OwnerController, Mode.OwnerController, "ServerRPC_ReadyoMove",direction,UGCGameSystem.GameState.PlayerInfos[teamIndex].TileChoosed);
            else
                Debug:Log("Invalid Tile")    
            end

            print("UIBP_MainUI:ChooseTile_Button_OnClicked. teamIndex  "..teamIndex.."  ")
        else
            print("UIBP_MainUI:ChooseTile_Button_OnClicked get TeamIndex failed");
        end
    else
        print("UIBP_MainUI:ChooseTile_Button_OnClicked get playerController failed");
    end

    -- self.UIBP_ChooseTile_0:SetVisibility(ESlateVisibility.Collapsed);
    -- -- 执行移动
    -- -- 请求移动棋子,在这里给方向和砖块赋值
    -- UnrealNetwork.CallUnrealRPC(Mode.OwnerController, Mode.OwnerController, "ServerRPC_ReadyoMove",direection,targetTile);

end
--
return UIBP_MainUI;